package com.atguigu.flink.chapter04_window.agg;

import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * Created by Smexy on 2022/10/25
 *
 *      通过过去每种传感器，每5个的vc总和
 *          滚动聚合。来一条聚合一次。
 *          sum
 *          max:  除了keyBy列和聚合列，其他列取第一条
 *          maxBy : 除了keyBy列和聚合列，其他列默认取最大聚合列
 *          min:  除了keyBy列和聚合列，其他列取第一条
 *          minBy: 除了keyBy列和聚合列，其他列默认取最小聚合列
 */
public class Demo10_Sum
{
    public static void main(String[] args) {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env
           .socketTextStream("hadoop103", 8888)
           .map(new MapFunction<String, WaterSensor>()
           {
               @Override
               public WaterSensor map(String value) throws Exception {
                   String[] data = value.split(",");
                   return new WaterSensor(
                       data[0],
                       Long.valueOf(data[1]),
                       Integer.valueOf(data[2])
                   );
               }
           })
           .keyBy(WaterSensor::getId)
           .countWindow(5)
           //.sum("vc").setParallelism(2)
           .maxBy("vc").setParallelism(2)
           .print();

        try {
                    env.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }

    }
}
